#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sets import Set
from random import randint
import itertools

def randrange(a,b):
    return randint(a,b)

#random test for primality from Solovay and Strassen
def solovay_strassen(n, k=10):
    if n == 2:
        return True
    if not n & 1:
        return False
 
    def legendre(a, p):
        if p < 2:
            raise ValueError('p must not be < 2')
        if (a == 0) or (a == 1):
            return a
        if a % 2 == 0:
            r = legendre(a / 2, p)
            if p * p - 1 & 8 != 0:
                r *= -1
        else:
            r = legendre(p % a, a)
            if (a - 1) * (p - 1) & 4 != 0:
                r *= -1
        return r
 
    for i in xrange(k):
        a = randrange(2, n - 1)
        x = legendre(a, n)
        y = pow(a, (n - 1) / 2, n)
        if (x == 0) or (y != x % n):
            return False
 
    return True


def primes_sieve(limit):
    limitn = limit+1
    not_prime = [False] * limitn
    primes = []

    for i in range(2, limitn):
        if not_prime[i]:
            continue
        for f in xrange(i*2, limitn, i):
            not_prime[f] = True

        primes.append(i)

    return primes



def isPanDigital(nDigitNumber):
    
    rednDigitNumber=nDigitNumber
    digiSet = Set()
    isPanDigitl=True
    while rednDigitNumber>0:
        lastDigit = rednDigitNumber % 10        
        if lastDigit in digiSet:
            isPanDigitl=False
        else: 
            digiSet.add(lastDigit)
        rednDigitNumber=(rednDigitNumber-lastDigit)/10
    return isPanDigitl

def ndigNumber(digArr):
    retVal=0
    for digit in digArr:
        retVal=10*retVal+digit
    return retVal
     
def pangDigPrimes(limit):
    primes = primes_sieve(limit)
    largestPangDigPrime=0
    for prime in primes:
        if isPanDigital(prime):
            #print prime
            if prime>largestPangDigPrime:
                largestPangDigPrime=prime
    return largestPangDigPrime





d = range(1,8)
#7652413
e = len(d)
primFound=0
for p in itertools.permutations(d, e):
    number=ndigNumber(p)
    #print number
    if solovay_strassen(number):        
        if number>primFound:
            primFound=number
if primFound:
    print "max found",primFound
else:
    print "nothing found"
     
